//
//  MKUIPickerViewTreeModel.m
//  MKUI
//
//  Created by sushangjin on 15/6/19.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import "MKUIPickerViewTreeModel.h"
#import "MKUIPickerViewTreeCellModel.h"

@interface MKUIPickerViewTreeModel()
@property(nonatomic,strong) NSArray *selectedNodePaths;//选中的结点路径列表@[MKUIPickerViewTreeNode]
@end
@implementation MKUIPickerViewTreeModel
- (NSArray *)rootNodes{
	return [_rootNodes copy];
}
- (void)setRootNodes:(NSArray *)rootNodes{
	[_rootNodes removeAllObjects];
	[_rootNodes addObjectsFromArray:rootNodes];
}
- (void)addRootNode:(MKUIPickerViewTreeNode *)node{
	if(!node)return;
	[_rootNodes addObject:node];
}
- (void)insertRootNode:(MKUIPickerViewTreeNode *)node atIndex:(NSInteger)index{
	if(!node)return;
	[_rootNodes insertObject:node atIndex:index];
}
- (void)removeRootNode:(MKUIPickerViewTreeNode *)node{
	if(!node)return;
	[_rootNodes removeObject:node];
}
- (void)removeRootNodeAtIndex:(NSInteger)index{
	if(index>=0&&index<_rootNodes.count){
		[_rootNodes removeObjectAtIndex:index];
	}
}
- (void)removeAllRootNodes{
	[_rootNodes removeAllObjects];
}
- (id)init{
	if (self=[super init]) {
		_rootNodes = [[NSMutableArray alloc] init];
	}
	return self;
}
- (id)initWithRootNodes:(NSArray *)rootNodes{
	if (self=[self init]) {
		[_rootNodes addObjectsFromArray:rootNodes];
	}
	return self;
}
- (void)reloadPickerViewData{
	[self _reloadPickerViewTreeData];
	[super reloadPickerViewData];
}
- (void)_reloadPickerViewTreeData{
	[self removeAllSectionModels];
	[self addSectionModel:[self createEmptySectionModel]];
	//添加第一级分组
	if(self.selectedNodePaths.count==0&&self.rootNodes.count>0){
		[self _selectNode:[self.rootNodes firstObject]];
	}
	MKTreeNode *rootSelectedNode = [self.selectedNodePaths firstObject];
	for (MKUIPickerViewTreeNode *n in self.rootNodes) {
		MKUIPickerViewTreeCellModel *cm = n.cellModel;
		cm.selected = [rootSelectedNode isEqual:n];
		[self addCellModel:cm];
	}
	for (int i=0; i<self.selectedNodePaths.count; i++) {
		MKTreeNode *n = self.selectedNodePaths[i];
		if(!n.isLeaf&&n.childrenNodes.count>0){
			[self addSectionModel:[self createEmptySectionModel]];
			MKTreeNode *selectedNode = i<self.selectedNodePaths.count-1?self.selectedNodePaths[i+1]:nil;
			for (MKUIPickerViewTreeNode *n1 in n.childrenNodes) {
				MKUIPickerViewTreeCellModel *cm = n1.cellModel;
				cm.selected = [selectedNode isEqual:n1];
				[self addCellModel:cm];
			}
		}
	}
}
- (void)_selectNodePath:(NSArray *)nodePath{
	MKTreeNode *lastNode = [nodePath lastObject];
	if([self.selectedNodePaths containsObject:lastNode]){
		return;
	}
	NSMutableArray *selectedNodePaths = [NSMutableArray arrayWithArray:nodePath];
	if(!lastNode){
		lastNode = [self.rootNodes firstObject];
		[selectedNodePaths addObject:lastNode];
	}
	while(!lastNode.isLeaf&&lastNode.childrenNodes.count>0){
		lastNode = [lastNode.childrenNodes firstObject];
		[selectedNodePaths addObject:lastNode];
	}
	self.selectedNodePaths = selectedNodePaths;
}
- (void)_selectNode:(MKTreeNode *)node{
	if([self.selectedNodePaths containsObject:node]){
		return;
	}
	NSArray *nodePath = [MKTreeNode nodePathOfNode:node rootNodes:self.rootNodes];
	[self _selectNodePath:nodePath];
}
- (void)selectCellModelsAtIndexPaths:(NSArray *)indexPaths{
	indexPaths = [indexPaths sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
		NSIndexPath *indexpath1 = (NSIndexPath *)obj1;
		NSIndexPath *indexpath2 = (NSIndexPath *)obj2;
		NSComparisonResult r = [indexpath1 compare:indexpath2];
		return r;
	}];
	if([self numberOfSections]==0){//没有数据
		[self _reloadPickerViewTreeData];
	}
	for (NSIndexPath *indexpath in indexPaths) {
		MKUIPickerViewCellModel *cm = [self cellModelAtIndexPath:indexpath];
		[self selectCellModel:cm];
		[self _reloadPickerViewTreeData];//重新生成模型数据
	}
}
- (void)selectCellModels:(NSArray *)cellModels{
	cellModels = [cellModels sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
		MKUIPickerViewTreeCellModel *cm1 = (MKUIPickerViewTreeCellModel *)obj1;
		MKUIPickerViewTreeCellModel *cm2 = (MKUIPickerViewTreeCellModel *)obj2;
		NSComparisonResult r = [cm1 compare:cm2];
		return r;
	}];
	for (MKUIPickerViewTreeCellModel *cm in cellModels) {
		[self selectCellModel:cm];
		[self _reloadPickerViewTreeData];//重新生成模型数据
	}
}
- (void)selectCellModel:(MKCollectionCellModel *)cellModel{
	[super selectCellModel:cellModel];
	MKUIPickerViewTreeCellModel *cm = (MKUIPickerViewTreeCellModel *)cellModel;
	[self _selectNode:cm.node];
}
- (void)selectNode:(MKTreeNode *)node passingTest:(BOOL(^)(MKTreeNode *n1,MKTreeNode *n2))passingTest{
	NSArray *nodePath = [MKTreeNode nodePathOfNode:node rootNodes:self.rootNodes passingTest:passingTest];
	[self _selectNodePath:nodePath];
	[self reloadPickerViewData];
}
- (void)selectNode:(MKTreeNode *)node{
	[self _selectNode:node];
	[self reloadPickerViewData];
}
- (NSArray *)indexPathsForCellModelsWithTitles:(NSArray *)titles{
	NSMutableArray *indexpaths = [[NSMutableArray alloc] initWithCapacity:titles.count];
	NSArray *nodes = self.rootNodes;
	for (int i=0; i<titles.count; i++) {
		if(nodes.count==0)break;
		NSString *title = titles[i];
		NSInteger index = [MKUIPickerViewTreeNode indexOfNodeWithTitle:title nodes:nodes];
		if(index!=NSNotFound){
			NSIndexPath *indexpath = [NSIndexPath indexPathForRow:index inSection:i];
			[indexpaths addObject:indexpath];
			MKUIPickerViewTreeNode *node = [nodes objectAtIndex:index];
			nodes = [node childrenNodes];
		}else{
			break;
		}
	}
	return indexpaths;
}
- (NSIndexPath *)indexPathForLastCellModelWithTitle:(NSString *)title{
	MKUIPickerViewTreeNode *node = [[MKUIPickerViewTreeNode alloc] initWithTitle:title];
	NSArray *path = [MKTreeNode nodePathOfNode:node rootNodes:self.rootNodes passingTest:^BOOL(MKTreeNode *n1, MKTreeNode *n2) {
		MKUIPickerViewTreeNode *node1 = (MKUIPickerViewTreeNode *)n1;
		MKUIPickerViewTreeNode *node2 = (MKUIPickerViewTreeNode *)n2;
		return [node1.cellModel.title isEqualToString:node2.cellModel.title];
	}];
	NSIndexPath *indexpath;
	if(path.count>0){
		MKUIPickerViewTreeNode *lastNode = [path lastObject];
		NSInteger sectionIndex = path.count-1;
		NSInteger rowIndex = path.count==1?[self.rootNodes indexOfObject:lastNode]:[lastNode.parentNode.childrenNodes indexOfObject:lastNode];
		indexpath = [NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex];
	}
	return indexpath;
}
- (MKUIPickerViewTreeCellModel *)lastCellModelWithTitle:(NSString *)title{
	MKUIPickerViewTreeNode *lastNode = [self lastNodeWithTitle:title];
	MKUIPickerViewTreeCellModel *cellModel = lastNode.cellModel;
	return cellModel;
}
- (MKUIPickerViewTreeNode *)lastNodeWithTitle:(NSString *)title{
	MKUIPickerViewTreeNode *node = [[MKUIPickerViewTreeNode alloc] initWithTitle:title];
	NSArray *path = [MKTreeNode nodePathOfNode:node rootNodes:self.rootNodes passingTest:^BOOL(MKTreeNode *n1, MKTreeNode *n2) {
		MKUIPickerViewTreeNode *node1 = (MKUIPickerViewTreeNode *)n1;
		MKUIPickerViewTreeNode *node2 = (MKUIPickerViewTreeNode *)n2;
		return [node1.cellModel.title isEqualToString:node2.cellModel.title];
	}];
	MKUIPickerViewTreeNode *lastNode = [path lastObject];
	return lastNode;
}
#pragma mark - delagate:UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
	MKUIPickerViewTreeCellModel *cellModel = (MKUIPickerViewTreeCellModel *)[self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
	MKTreeNode *node = cellModel.node;
	[self selectNode:node];
	//由于selectNode会重构model数据,因此要重新获取cellModel
	cellModel = (MKUIPickerViewTreeCellModel *)[self cellModelAtIndexPath:[NSIndexPath indexPathForRow:row inSection:component]];
	[cellModel didSelected:pickerView];
	if([self.forwardDelegate respondsToSelector:_cmd]){
		[self.forwardDelegate pickerView:pickerView didSelectRow:row inComponent:component];
	}
}
@end
